//存储信息
// const state = () => ({
//   loginUser: {
//     userName: "空",
//   },
// });

//getters 便捷获取对象的方法
// const getters = {};

//actions  执行异步操作，调用mutations修改
// const actions = {
//     getLoginUser({commit,state},payload){
//         commit("updateUser",{userName:"sandwich"})
//     }
// };

//mutations  修改状态变量 ，尽量同步
// const mutations = {
//   updateUser(state, payload) {
//       state.loginUser = payload ;
//   },
// };

// initial state
import { StoreOptions } from "vuex";
import { UserControllerService } from "../../generated";
import ACCESS_ENUM from "@/access/accessEnum";
import { LoginUserVO } from "../../generated/models/LoginUserVO";

// 定义当前模块的 State 类型
interface UserState {
  loginUser: LoginUserVO;
}
export default {
  namespaced: true,
  state: () => ({
    loginUser: {
      userName: undefined,
      userRole: ACCESS_ENUM.NOT_LOGIN,
    },
  }),
  actions: {
    async getLoginUser({ commit, state }, payload) {
      // 从远程请求获取登录信息
      const res = await UserControllerService.getLoginUserUsingGet();
      console.log("请求LoginUserVO" + res.data);
      if (res.code === 0) {
        commit("updateUser", res.data);
      } else {
        commit("updateUser", {
          ...state.loginUser,
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
    },
    async logout({ commit, state }, payload) {
      // 从远程请求获取登录信息
      const res = await UserControllerService.getLoginUserUsingGet();
      console.log("请求LoginUserVO" + res.data);
      if (res.code === 0) {
        commit("updateUser", res.data);
      } else {
        commit("updateUser", {
          ...state.loginUser,
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
    },
  },
  mutations: {
    updateUser(state, payload) {
      state.loginUser = payload;
    },
  },
} as StoreOptions<any>;
